package com.sinosoft.perfect.system._resource.action;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import com.sinosoft.perfect.common.system.BaseAction;
import com.sinosoft.perfect.system._.po.TResourceSystem;
import com.sinosoft.perfect.system._.vo.LoginUser;
import com.sinosoft.perfect.system._resource.dataFormat.IResourceGridDataFormat;
import com.sinosoft.perfect.system._resource.dataFormat.IResourceTreeDataFormat;
import com.sinosoft.perfect.system._resource.service.IResourceService;

@SuppressWarnings("serial")
public class ResourceAction extends BaseAction {

	private IResourceService resourceService;

	private IResourceGridDataFormat resourceGridDataFormat;

	private IResourceTreeDataFormat resourceTreeDataFormat;

	private TResourceSystem tresourcesystem;

	private String resourceId;

	private String[] resourceIds;
	
	
	public IResourceService getResourceService() {
		return resourceService;
	}

	public void setResourceService(IResourceService resourceService) {
		this.resourceService = resourceService;
	}

	public IResourceGridDataFormat getResourceGridDataFormat() {
		return resourceGridDataFormat;
	}

	public void setResourceGridDataFormat(
			IResourceGridDataFormat resourceGridDataFormat) {
		this.resourceGridDataFormat = resourceGridDataFormat;
	}

	public IResourceTreeDataFormat getResourceTreeDataFormat() {
		return resourceTreeDataFormat;
	}

	public void setResourceTreeDataFormat(
			IResourceTreeDataFormat resourceTreeDataFormat) {
		this.resourceTreeDataFormat = resourceTreeDataFormat;
	}

	public TResourceSystem getTresourcesystem() {
		return tresourcesystem;
	}

	public void setTresourcesystem(TResourceSystem tresourcesystem) {
		this.tresourcesystem = tresourcesystem;
	}

	public String getResourceId() {
		return resourceId;
	}

	public void setResourceId(String resourceId) {
		this.resourceId = resourceId;
	}

	public String[] getResourceIds() {
		return resourceIds;
	}

	public void setResourceIds(String[] resourceIds) {
		this.resourceIds = resourceIds;
	}

	/**
	 * 构建资源树
	 */
	public String resourceTree() throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		String resultXml = null;
		try {
			List<TResourceSystem> resources = resourceService.selectTResource(tresourcesystem);
			resultXml = resourceTreeDataFormat.dataFormatResourceTree(resources);

		} catch (Exception e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		PrintWriter out = response.getWriter();
		out.print(resultXml);
		out.flush();
		out.close();
		return null;
	}

	public String resourceGrid() throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		String resultXml = null;
		try {
			List<TResourceSystem> resources = resourceService.selectTResource(tresourcesystem);
			resultXml = resourceGridDataFormat.dataFormatResourceGrid(resources);

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		PrintWriter out = response.getWriter();
		out.print(resultXml);
		out.flush();
		out.close();
		return null;
	}

	public String resourceQuery() {
		try {
			if (tresourcesystem == null)
				tresourcesystem = new TResourceSystem();
			List<TResourceSystem> resources = resourceService.selectTResource(tresourcesystem);
			request.setAttribute("resources", resources);
		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}

		return "resourceList";
	}

	public String resourceQueryForEdit() {
		try {
			TResourceSystem resource = new TResourceSystem();
			resource.setResourceId(resourceId);

			List<TResourceSystem> resources = resourceService.selectTResource(resource);
			if (resources != null && resources.size() != 0) {
				request.setAttribute("resourceEdit", resources.get(0));
			}
		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		return "resourceEdit";
	}

	public String resourceEdit() {
		try {
			resourceService.updateTResourceByPK(tresourcesystem);
			this.resourceId = tresourcesystem.getResourceId();// 更新后，通过resourceId查询
		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		return resourceQueryForEdit();
	}

	public String resourceDelFromTree() {
		try {
			resourceService.deleteTResources(new String[] { resourceId });
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	
	public String resourceImgs() throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		String resultXml = null;
		try {
			String addr = ((LoginUser)session.get("user")).getThemeAddr();
			String resourceImg = request.getSession().getServletContext().getRealPath("theme"+File.separator+addr+File.separator+"resourceImgs");
			List<String> names = resourceService.getResourceImgsName(resourceImg);
			resultXml = resourceGridDataFormat.dataFormatResourceImgGrid(names,"theme/"+addr+"/resourceImgs/");
		} catch (Exception e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		PrintWriter out = response.getWriter();
		out.print(resultXml);
		out.flush();
		out.close();
		return null;
	}
}
